<?php

namespace app\admin\common;

class Week
{
	function get_week_all($year)
    {
        $whole_week = $this->get_big_week($year);//获取最大周数
        $str = '';
        for ($i = 1; $i <= $whole_week; $i++) {
            $str .= $i . ',';
        }
        $str_arr = explode(',', rtrim($str, ','));//转成数组
        $week_list = array();
        foreach ($str_arr as $key => $value) {
            $week_list[$key]['num'] = $value;
            $week = $this->get_year_month_week($year, $value);//一周的日期
            $now_date = date('Y-m-d');
           	if (in_array($now_date,$week)) {
           		$week_list[$key]['now'] = true;
           	}else{
           		$week_list[$key]['now'] = false;
           	}
            $week_list[$key]['detail'] = "第" . $value . "周";
            $week_list[$key]['fanwei'] = $week[0] . "至" . $week[6];
        }
        return $week_list;
    }
    /*
    用法:
    $this->get_date("Y-m-d","+1 day");当前日期加一天
    $this->get_date("Y-m-d","+1 month",$date);指定日期下一月
    $this->get_date("Y-m","+1 years",$date);指定日期下一年的年月
    $this->get_date("Y-m-t",NULL,$date);指定日期的月底
    $this->get_date("m","+1 month",$date);指定日期下一个月的月份(获取月份时候，日期必填。不然个别日期会有bug存在)
    等等，根据需求自己获取。代码里面strtotime获取日期的地方都会变成公共方法调用。
    */
    function get_date($return, $string, $date)
    {
        if (empty($return)) {
            $return = "Y-m-d";
        }
        if (empty($string)) {
            $string = "now";
        }
        if (empty($date)) {
            $date = time();
        } else {
            $date = strtotime(date($date));
        }
        if (preg_match("/month/", $string)) {
            $first_day_of_month = date('Y-m', $date) . '-01';
            $str_date = strtotime($first_day_of_month);
        }
        if (preg_match("/month/", $string)) {
            $l_date = date("Y-m-d", strtotime($string, $str_date));
            $day = substr(date("Y-m-d", $date), 8, 2);
            if (substr($l_date, 0, 7) . '-' . date('t', strtotime(substr($l_date, 0, 7))) < substr($l_date, 0, 7) . '-' . $day) {
                $l_date = substr($l_date, 0, 7) . '-' . date('t', strtotime(substr($l_date, 0, 7)));
            } else {
                $l_date = substr($l_date, 0, 7) . '-' . $day;
            }
            $l_date = date($return, strtotime($l_date));
            return $l_date;
        } else {
            return date($return, strtotime($string, $date));
        }
    }

    function get_big_week($year_now)
    {
        $big_week = $this->get_date("W", NULL, $year_now . '-12-31');
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-30');
        }
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-29');
        }
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-28');
        }
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-27');
        }
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-26');
        }
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-25');
        }
        if ($big_week == 1) {
            $big_week = $this->get_date("W", NULL, $year_now . '-12-24');
        }
        return $big_week;
    }

    function get_year_month_week($year_now, $week_now)
    {
        //指定年和周的日期数组
        $time = new \DateTime();
        $time->setISODate($year_now, $week_now, 1);
        $result[] = $time->format('Y-m-d');
        $time->setISODate($year_now, $week_now, 2);
        $result[] = $time->format('Y-m-d');
        $time->setISODate($year_now, $week_now, 3);
        $result[] = $time->format('Y-m-d');
        $time->setISODate($year_now, $week_now, 4);
        $result[] = $time->format('Y-m-d');
        $time->setISODate($year_now, $week_now, 5);
        $result[] = $time->format('Y-m-d');
        $time->setISODate($year_now, $week_now, 6);
        $result[] = $time->format('Y-m-d');
        $time->setISODate($year_now, $week_now, 7);
        $result[] = $time->format('Y-m-d');
        return $result;
    }

    function get_month(){
			$z = date('Y-m');

			$a = date('Y-m', strtotime('0 months'));

			$begin = new \DateTime($a);

			$end = new \DateTime($z);

			$end = $end->modify('+12 month');

			$interval = new \DateInterval('P1M');

			$daterange = new \DatePeriod($begin, $interval ,$end);
			$data =[];
			foreach($daterange as $key=>$date){
				$data[$key]['num'] = date('n');
			 	$data[$key]['detail'] = $date->format("Y-m");
			 	$now_date = date('Y-m');
				if ($now_date == $date->format("Y-m")) {
			           $data[$key]['now'] = true;
			    }else{
			           $data[$key]['now'] = false;
			   }
			   	$list['sel_time'] = $date->format("Y-m");
				$list['begin_time'] = strtotime($list['sel_time']);
				$list['end_time'] = mktime(23,59,59,date('m',strtotime($list['sel_time']))+1,00);
			 	$data[$key]['fanwei'] =date('Y-m-d',$list['begin_time']).'至'.date('Y-m-d',$list['end_time']);
			}
			return $data;
    }


    //计算两个时间戳相差天数
    function timediff($begin_time, $end_time)
    {
        if ($begin_time < $end_time) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        //计算小时数
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        //计算分钟数
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        //计算秒数
        $secs = $remain % 60;
        $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
        return $res;
    }


    //获取时长
    function hours_min($start_time,$end_time){
        if (strtotime($start_time) > strtotime($end_time)) list($start_time, $end_time) = array($end_time, $start_time);
        $sec = strtotime($end_time) - strtotime($start_time);
        $sec = round($sec/60);
        $min = str_pad($sec%60, 2, 0, STR_PAD_LEFT);
        $hours_min = floor($sec/60);
        $min != 0 && $hours_min;
        return $hours_min;
    }
}